package com.xinyue.web;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xinyue.dao.userDao;
import com.xinyue.dao.userDaoImpl;
import com.xinyue.model.user;
import com.xinyue.service.userService;

@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public loginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().invalidate();
		response.sendRedirect(request.getContextPath()+"/login/index.jsp");
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String path=request.getContextPath();
		int   id = Integer.parseInt(request.getParameter("account") + "username");
		String pass= request.getParameter("password");
		userDao ud = new userDaoImpl();
		ArrayList<user> us =ud.getList();
		//userService us=new userService();
		int flag=0;
		for(int i=0;i<us.size();i++) {
			user uu=new user();
			Object aObject=us.get(i);
			uu=(user)aObject;
			if(id==uu.getId()&&pass==uu.getPass()) {
				flag=1;
				break;
			}
			
		}
		if(flag==0) {
			request.setAttribute("id", id);
		    request.getRequestDispatcher("/login/el.jsp").forward(request, response);
		}
		else {
			request.getSession().setAttribute("id", id);
		    response.sendRedirect(path+"/index.jsp");
		}
		
		
	}

}
